package com.joven.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.joven.model.ClassRoom;
import com.joven.model.Student;
import com.joven.model.Teacher;
import com.joven.service.ClassRoomService;
import com.joven.service.StudentService;
import com.joven.service.TeacherService;

@Controller
@RequestMapping("/hb.do")
public class HBController  {
	
	@Resource(name="classRoomServiceImp")
	private ClassRoomService classRoomService;
	
	@Resource(name="teacherServiceImp")
	private TeacherService teacherService;
	
	@Resource(name="studentServiceImp")
	private StudentService studentService;
	
    //到录入页
	@RequestMapping(params="method=input")
	public String toInput(ModelMap model,HttpServletRequest request, HttpServletResponse response){
		findAll(model, request, response);
		return "hb/input";
	}
	
	//添加教室
	@RequestMapping(params="method=addclassroom")
	public String addClassRome(ClassRoom classRoom,ModelMap model,HttpServletRequest request, HttpServletResponse response){
		classRoomService.save(classRoom);
		findAll(model, request, response);
		return "redirect:hb.do?method=input";
	}
	
	//添加老师
	@RequestMapping(params="method=addteacher")
	public String addTeacher(Teacher teacher,ModelMap model,HttpServletRequest request, HttpServletResponse response){
		if (teacher.getClassRoom()!=null){
			if (teacher.getClassRoom().getRoomId().equals("")){
				teacher.setClassRoom(null);
			}
		}
		teacherService.save(teacher);
		findAll(model, request, response);
		return "redirect:hb.do?method=input";
	}
	
	//添加学生
	@RequestMapping(params="method=addstudent")
	public String addStudent(Student student,ModelMap model,HttpServletRequest request, HttpServletResponse response){
		if (student.getTeacher().getTcid().equals("")){
				student.setTeacher(null);
			}
		if ("".equals(request.getParameter("classRoom.roomId"))){
			student.setClassRoom(null);
		}	
		
		
		studentService.save(student);
		findAll(model, request, response);
		return "redirect:hb.do?method=input";
	}
	
	//注入多个学生
	@RequestMapping(params="method=addstudents")
	public String addStudents(ClassRoom classRoom,ModelMap model,HttpServletRequest request, HttpServletResponse response) throws Exception{
		List<Student> stulst=classRoom.getStudents();
		for (Student stu:stulst){
			stu.setClassRoom(classRoom);
		}
		studentService.saveAll(stulst);
		findAll(model, request, response);
		return "redirect:hb.do?method=input";
	}
	
	//删除教室
	@RequestMapping(params="method=delete")
	public String delete(ModelMap model,HttpServletRequest request, HttpServletResponse response){
		String id=request.getParameter("id");
		String type=request.getParameter("type");
		
		if ("c".equals(type)){
			classRoomService.deleteById(id);
		}else if ("t".equals(type)){
			teacherService.deleteById(id);
		}else if("s".equals(type)){
			studentService.deleteById(id);
		}
		
		findAll(model, request, response);
		return "redirect:hb.do?method=input";
	}
	
	
	//到结果页
	@RequestMapping(params="method=result")
	public String toGetData(ModelMap model,HttpServletRequest request, HttpServletResponse response){
		
		return "hb/result";
	}
	
	private void findAll(ModelMap model,HttpServletRequest request, HttpServletResponse response){
		List<ClassRoom> roomlst=classRoomService.getAll();
		List<Teacher> teacherlst=teacherService.getAll();
		List<Student> studentlst=studentService.getAll();
		
		model.put("roomlst", roomlst);
		model.put("teacherlst", teacherlst);
		model.put("studentlst",studentlst);
		
	}

}
